const router = require('koa-router')()
const { query } = require("../db")
const { URL } = require("url");
const path = require("path");
const fs = require("fs");
router.prefix('/like');


//用户点击喜欢时,发送post请求,把用户信息(user_id)和喜欢内容(occult_id),img_url,title存入数据库的like_occult表中
router.post('/',async ctx=> {
    let user_id = ctx.request.body.user_id
  let occult_id = ctx.request.body.occult_id
  let img_url = ctx.request.body.img_url
  let title = ctx.request.body.title
  let sql = "insert into like_occult (user_id, occult_id,img_url,title) values('" + user_id + "', '" + occult_id + "','"+img_url+"','"+title+"')";
  await query(sql, [])      
})

//取消喜欢
router.post('/delete',async ctx=>{
      let user_id = ctx.request.body.user_id
      let occult_id = ctx.request.body.occult_id
      let sql = "delete from like_occult where user_id='"+ user_id + "'and occult_id='" + occult_id+"'"
      await query(sql, [])
  
  })

//更改喜欢的数量
router.post('/changelike',async ctx=>{
    let num = ctx.request.body.num
    let id = ctx.request.body.id
    let sql = "update occult set num='"+num+"' where id='"+id+"'"
    await query(sql, [])
})


//获取like_occult表中的数据
router.post('/person_like',async ctx=>{
    let user_id = ctx.request.body.user_id
    let sql = "select * from like_occult where user_id='"+user_id+"'";
    let results = await query(sql, []);
    ctx.res.end(JSON.stringify(results));
})

module.exports = router